package com.hiwan.unions.activity.membership;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.adapter.bean.MembersAdresssBean;
import com.hiwan.unions.adapter.bean.MembersBean;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.ToastUtils;
import com.hiwan.unions.util.Utils;
import com.hiwan.unions.view.popupWindow.MembersListPop;
import com.hiwan.unions.view.popupWindow.MembershipPop;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by llg on 2017/8/11.
 */
public class MembershipTransferActivity extends RootActivity implements MembershipPop.OnTextLinstener, MembersListPop.OnTextListLinstener, HttpListener<JSONObject> {
    Request<JSONObject> request;
    private ArrayList<MembersBean> arry_wh = new ArrayList<MembersBean>();
    private ArrayList<String> arry_years = new ArrayList<String>();
    private ArrayList<MembersAdresssBean> arry_years1 = new ArrayList<MembersAdresssBean>();
    private ArrayList<MembersAdresssBean> arry_years2 = new ArrayList<MembersAdresssBean>();
    private ArrayList<MembersAdresssBean> arry_years3 = new ArrayList<MembersAdresssBean>();
    private TextView edit_address_1, edit_address_2, edit_address_3, edit_wh;
    private EditText edit_address_4, edit_gz, edit_zw;
    private String code4 = "", code4_1 = "", code5 = "", code6 = "";
    private MembersListPop listPop;
    private int i = 0;
    private View view;
    private MembershipPop membershipPop;
    JSONArray jsonArray;
    MembersAdresssBean bean;
    private LinearLayout layout;

    public static void startPreview(Context context) {
        Intent intent = new Intent(context, MembershipTransferActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_membership_transfer2;
    }

    @Override
    protected void initData() {
        bundle = getIntent().getExtras();
        stringArrayList(Utils.getJson(this, "yuanyin.txt"), arry_wh);
        request = NoHttp.createJsonObjectRequest(ConstsNet.getAreaList, RequestMethod.POST);
        request.add("levelType", "1");
        CallServer.getRequestInstance().addToken(this, 2, request, this, true, userInfo);
    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        edit_address_1 = findView(R.id.edit_address_1);
        edit_address_2 = findView(R.id.edit_address_2);
        edit_address_3 = findView(R.id.edit_address_3);
        edit_address_4 = findView(R.id.edit_address_4);
        edit_gz = findView(R.id.edit_gz);
        edit_wh = findView(R.id.edit_wh);
        layout = findView(R.id.layout);
        edit_zw = findView(R.id.edit_zw);
        setTitleTxt("我要转会");
        setOperationBtn("提交");
        membershipPop = new MembershipPop(this);
        membershipPop.setOnTextLinstener(this);
    }

    @Override
    public void getOperationBtn() {
        super.getOperationBtn();
        request = NoHttp.createJsonObjectRequest(ConstsNet.updateTransfer, RequestMethod.POST);
        try {

            request.add("reason", edit_wh.getText().toString().trim());
            if (CheckUtils.checkBoolean(edit_wh, "劳动（工作）关系发生变化")) {
                request.add("company", CheckUtils.check(edit_gz, getString(R.string.enter_company_error2)));
                request.add("post", CheckUtils.check(edit_zw, getString(R.string.enter_job_error2)));
                request.add("company_city", code4_1);
                request.add("company_district", code4);
                request.add("company_street", code5);
                request.add("company_community", code6);
                request.add("detailed_address", CheckUtils.check(edit_address_4, getString(R.string.enter_address4_error2)));
                CheckUtils.check(edit_address_2, getString(R.string.enter_address2_error2));
                CheckUtils.check(edit_address_3, getString(R.string.enter_address3_error2));
                CheckUtils.check(edit_address_4, getString(R.string.enter_address4_error2));
            }
            request.add("optiontype", fo2(arry_wh, edit_wh));
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }
        CallServer.getRequestInstance().addToken(this, 0, request, this, true, userInfo);

    }

    private String fo2(ArrayList<MembersBean> list, TextView textView) {
        for (int i = 0; i < list.size(); i++) {
            MembersBean bean = list.get(i);
            if (CheckUtils.checkBoolean(textView, bean.getTitle())) {
                return bean.getId();
            }
        }
        return "";
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        JSONObject jsonObject = response.get();
        switch (what) {
            case 0:
                ToastUtils.showToast(this, "已提交，我们会尽快处理");
                MembershipshenheActivity.startPreview2(this, "会籍消息");
                finish();
                break;
            case 2:
                jsonArray = JsonUtil.getJSONArray(jsonObject, "data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                    bean = new MembersAdresssBean();
                    bean.setArea_code(JsonUtil.getString(jsonObject, "area_code"));
                    bean.setArea_name(JsonUtil.getString(jsonObject, "area_name"));
                    bean.setLevel(JsonUtil.getString(jsonObject, "level"));
                    bean.setParent_area_code(JsonUtil.getString(jsonObject, "parent_area_code"));
                    arry_years1.add(bean);
                }
                break;
            case 3:
                arry_years2.clear();
                jsonArray = JsonUtil.getJSONArray(jsonObject, "data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                    bean = new MembersAdresssBean();
                    bean.setArea_code(JsonUtil.getString(jsonObject, "area_code"));
                    bean.setArea_name(JsonUtil.getString(jsonObject, "area_name"));
                    bean.setLevel(JsonUtil.getString(jsonObject, "level"));
                    bean.setParent_area_code(JsonUtil.getString(jsonObject, "parent_area_code"));
                    arry_years2.add(bean);
                }
                bean = new MembersAdresssBean();
                bean.setArea_code("000000");
                bean.setArea_name("不确定");
                bean.setLevel("2");
                bean.setParent_area_code("000000");
                arry_years2.add(bean);
                OnClicks(view, stringArrayList2(arry_years2), edit_address_2.getText().toString());
                break;
            case 4:
                arry_years3.clear();
                jsonArray = JsonUtil.getJSONArray(jsonObject, "data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                    MembersAdresssBean bean = new MembersAdresssBean();
                    bean.setArea_code(JsonUtil.getString(jsonObject, "area_code"));
                    bean.setArea_name(JsonUtil.getString(jsonObject, "area_name"));
                    bean.setLevel(JsonUtil.getString(jsonObject, "level"));
                    bean.setParent_area_code(JsonUtil.getString(jsonObject, "parent_area_code"));
                    arry_years3.add(bean);
                }
                bean = new MembersAdresssBean();
                bean.setArea_code("000000");
                bean.setArea_name("不确定");
                bean.setLevel("2");
                bean.setParent_area_code("000000");
                arry_years3.add(bean);
                OnClicks(view, stringArrayList2(arry_years3), edit_address_3.getText().toString());
                break;
        }
    }

    private ArrayList<String> stringArrayList2(ArrayList<MembersAdresssBean> strings) {
        arry_years.clear();
        for (MembersAdresssBean s : strings) {
            arry_years.add(s.getArea_name());
        }
        return arry_years;
    }


    public void ButtonOnClick(View view) {
        switch (view.getId()) {
            case R.id.wh_layout:
                i = 0;
                OnClicks(view, stringArrayList(arry_wh), edit_wh.getText().toString());
                break;
            case R.id.layout_address_1:
                if (listPop == null) {
                    listPop = new MembersListPop(this, arry_years1);
                    listPop.setOnTextLinstener(this);
                }
                listPop.showPopupWindow(view, code4_1, code4);
                break;
            case R.id.layout_address_2:
                this.view = view;
                i = 4;
                setList(code4, 3);
                break;
            case R.id.layout_address_3:
                this.view = view;
                i = 5;
                setList(code5, 4);
                break;

        }
    }

    private void setList(String parent_area_code, int i) {
        if (CheckUtils.isEmpty(parent_area_code)) {
            String s;
            if (i == 3) {
                s = "请选择地市/区县";
            } else {
                s = "请选择街道";
            }
            ToastUtils.showToast(this, s);
            return;
        }
        if (i == 5) {
            if (CheckUtils.checkBoolean(edit_address_3, "不确定")) {
                return;
            }
        }
        request = NoHttp.createJsonObjectRequest(ConstsNet.getAreaList, RequestMethod.POST);
        request.add("levelType", "2");
        request.add("parent_area_code", parent_area_code);
        CallServer.getRequestInstance().addToken(this, i, request, this, true, userInfo);
    }


    private void OnClicks(View view, ArrayList<String> list, String content) {
        if (list.size() == 0) {
            ToastUtils.showToast(this, "暂无数据选择");
            return;
        }
        membershipPop.showPopupWindow(view, list, content);
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {

    }

    @Override
    public void OnText(String text, int z) {
        if (i == 0) {
            edit_wh.setText(text);
            if (CheckUtils.checkBoolean(edit_wh, "劳动（工作）关系发生变化")) {
                layout.setVisibility(View.VISIBLE);
            } else {
                layout.setVisibility(View.GONE);
            }
        } else if (i == 4) {
            code5 = arry_years2.get(z).getArea_code();
            code6 = "";
            edit_address_3.setText("");
            if (text.equals("不确定")) {
                code6 = code5;
                edit_address_3.setText("不确定");
            }
            edit_address_2.setText(text);
        } else if (i == 5) {
            Logger.i(" arry_years3.get(z).getArea_code()" + arry_years3.get(z).getArea_code());
            code6 = arry_years3.get(z).getArea_code();
            edit_address_3.setText(text);
        }
    }

    @Override
    public void OnDateText(String id, String text, String id2, String text2) {
        Logger.i("text   " + text + text2);
        String addre = text + text2;
        if (addre.equals(edit_address_1.getText().toString())) {
            return;
        }
        code4_1 = id;
        code4 = id2;
        edit_address_1.setText(text + text2);
        edit_address_2.setText("");
        edit_address_3.setText("");
    }

    private ArrayList<String> stringArrayList(ArrayList<MembersBean> strings) {
        arry_years.clear();
        for (MembersBean s : strings) {
            arry_years.add(s.getTitle());
        }
        return arry_years;
    }

    private void stringArrayList(String s, ArrayList<MembersBean> strings) {
        try {
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                MembersBean bean = new MembersBean();
                bean.setId(JsonUtil.getString(jsonObject, "id"));
                bean.setTitle(JsonUtil.getString(jsonObject, "title"));
                strings.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}